//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LocalVolSurface.h"
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVolTermStructure.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/LocalVolTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::CLocalVolSurface (Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ blackTS, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::IYieldTermStructure^ dividendTS, Double underlying) : CLocalVolTermStructure(CLocalVolSurface::typeid)
{
    CBlackVolTermStructure^ _CblackTS;
    CYieldTermStructure^ _CriskFreeTS;
    CYieldTermStructure^ _CdividendTS;
    try
    {
#ifdef HANDLE
        _phLocalVolSurface = NULL;
#endif
        _CblackTS = safe_cast<CBlackVolTermStructure^> (blackTS);
        _CblackTS->Lock();
        Handle<QuantLib::BlackVolTermStructure>& _blackTS = static_cast<Handle<QuantLib::BlackVolTermStructure>&> (_CblackTS->GetHandle ()); 
        _CriskFreeTS = safe_cast<CYieldTermStructure^> (riskFreeTS);
        _CriskFreeTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _riskFreeTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CriskFreeTS->GetHandle ()); 
        _CdividendTS = safe_cast<CYieldTermStructure^> (dividendTS);
        _CdividendTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _dividendTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdividendTS->GetHandle ()); 
        QuantLib::Real _underlying = (QuantLib::Real)ValueHelper::Convert (underlying); //d
        _ppLocalVolSurface = new boost::shared_ptr<QuantLib::LocalVolSurface> (new QuantLib::LocalVolSurface ( _blackTS,  _riskFreeTS,  _dividendTS,  _underlying ));
        SetLocalVolTermStructure (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolSurface));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CblackTS != nullptr) _CblackTS->Unlock();
        if (_CriskFreeTS != nullptr) _CriskFreeTS->Unlock();
        if (_CdividendTS != nullptr) _CdividendTS->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::CLocalVolSurface (Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ blackTS, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::IYieldTermStructure^ dividendTS, Cephei::QL::IQuote^ underlying) : CLocalVolTermStructure(CLocalVolSurface::typeid)
{
    CBlackVolTermStructure^ _CblackTS;
    CYieldTermStructure^ _CriskFreeTS;
    CYieldTermStructure^ _CdividendTS;
    CQuote^ _Cunderlying;
    try
    {
#ifdef HANDLE
        _phLocalVolSurface = NULL;
#endif
        _CblackTS = safe_cast<CBlackVolTermStructure^> (blackTS);
        _CblackTS->Lock();
        Handle<QuantLib::BlackVolTermStructure>& _blackTS = static_cast<Handle<QuantLib::BlackVolTermStructure>&> (_CblackTS->GetHandle ()); 
        _CriskFreeTS = safe_cast<CYieldTermStructure^> (riskFreeTS);
        _CriskFreeTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _riskFreeTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CriskFreeTS->GetHandle ()); 
        _CdividendTS = safe_cast<CYieldTermStructure^> (dividendTS);
        _CdividendTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _dividendTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdividendTS->GetHandle ()); 
        _Cunderlying = safe_cast<CQuote^> (underlying);
        _Cunderlying->Lock();
        Handle<QuantLib::Quote>& _underlying = static_cast<Handle<QuantLib::Quote>&> (_Cunderlying->GetHandle ()); 
        _ppLocalVolSurface = new boost::shared_ptr<QuantLib::LocalVolSurface> (new QuantLib::LocalVolSurface ( _blackTS,  _riskFreeTS,  _dividendTS,  _underlying ));
        SetLocalVolTermStructure (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolSurface));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CblackTS != nullptr) _CblackTS->Unlock();
        if (_CriskFreeTS != nullptr) _CriskFreeTS->Unlock();
        if (_CdividendTS != nullptr) _CdividendTS->Unlock();
        if (_Cunderlying != nullptr) _Cunderlying->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::CLocalVolSurface (boost::shared_ptr<QuantLib::LocalVolSurface>& childNative, Object^ owner) : CLocalVolTermStructure(CLocalVolSurface::typeid)
{
#ifdef HANDLE
	_phLocalVolSurface = NULL;
#endif
	_ppLocalVolSurface = &childNative;
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolSurface));
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::CLocalVolSurface (QuantLib::LocalVolSurface& childNative, Object^ owner) : CLocalVolTermStructure(CLocalVolSurface::typeid)
{
#ifdef HANDLE
	_phLocalVolSurface = NULL;
#endif
	_ppLocalVolSurface = new boost::shared_ptr<QuantLib::LocalVolSurface> (&childNative);
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolSurface));
    _LocalVolSurfaceOwner = owner;
    _LocalVolTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::CLocalVolSurface (CLocalVolSurface^ copy) : CLocalVolTermStructure(CLocalVolSurface::typeid)
{
#ifdef HANDLE
	_phLocalVolSurface = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLocalVolSurface = new boost::shared_ptr<QuantLib::LocalVolSurface> (copy->GetShared());
        _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolSurface));
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::CLocalVolSurface (PLATFORM::Type^ t) : CLocalVolTermStructure(CLocalVolSurface::typeid)
{
#ifdef HANDLE
	_phLocalVolSurface = NULL;
#endif
	if (!t->IsSubclassOf(CLocalVolSurface::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::CLocalVolSurface (QuantLib::Handle<QuantLib::LocalVolSurface>& childNative, Object^ owner)  : CLocalVolTermStructure(CLocalVolSurface::typeid)
{
	_phLocalVolSurface = &childNative;
	_ppLocalVolSurface = &static_cast<boost::shared_ptr<QuantLib::LocalVolSurface>>(childNative.currentLink());
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolSurface));
    _LocalVolSurfaceOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::CLocalVolSurface (QuantLib::Handle<QuantLib::LocalVolSurface> childNative)  : CLocalVolTermStructure(CLocalVolSurface::typeid)
{
	_phLocalVolSurface = &childNative;
	_ppLocalVolSurface = &static_cast<boost::shared_ptr<QuantLib::LocalVolSurface>>(childNative.currentLink());
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolSurface));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::CLocalVolSurface (QuantLib::LocalVolSurface childNative)  : CLocalVolTermStructure(CLocalVolSurface::typeid)
{
#ifdef HANDLE
	_phLocalVolSurface = NULL;
#endif
	_ppLocalVolSurface = new boost::shared_ptr<QuantLib::LocalVolSurface> (new QuantLib::LocalVolSurface (childNative));
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolSurface));
}
#endif

Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::~CLocalVolSurface ()
{
    if (_ppLocalVolSurface != NULL)
    {
	    delete _ppLocalVolSurface;
        _ppLocalVolSurface = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::!CLocalVolSurface ()
{
    if (_ppLocalVolSurface != NULL)
    {
	    delete _ppLocalVolSurface;
    }
}
QuantLib::LocalVolSurface& Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::GetReference ()
{
    if (_ppLocalVolSurface == NULL) throw REFNEW NativeNullException ();
	return **_ppLocalVolSurface;
}
boost::shared_ptr<QuantLib::LocalVolSurface>& Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::GetShared ()
{
    if (_ppLocalVolSurface == NULL) throw REFNEW NativeNullException ();
	return *_ppLocalVolSurface;
}
QuantLib::LocalVolSurface* Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::GetPointer ()
{
    if (_ppLocalVolSurface == NULL) throw REFNEW NativeNullException ();
	return &**_ppLocalVolSurface;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LocalVolSurface>& Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::GetHandle ()
{
	if (_phLocalVolSurface == NULL)
	{
		_phLocalVolSurface = new Handle<QuantLib::LocalVolSurface> (*_ppLocalVolSurface);
	}
	return *_phLocalVolSurface;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::HasNative () 
{
	return (_ppLocalVolSurface != NULL);
}

Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppLocalVolSurface)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppLocalVolSurface)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLocalVolSurface)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLocalVolSurface)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppLocalVolSurface)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Equityfx::ILocalVolSurface^ Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface_Factory::Create (Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ blackTS, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::IYieldTermStructure^ dividendTS, Double underlying)
{
    return REFNEW CLocalVolSurface ( blackTS,  riskFreeTS,  dividendTS,  underlying);
}
Cephei::QL::Termstructures::Volatility::Equityfx::ILocalVolSurface^ Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolSurface_Factory::Create (Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ blackTS, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::IYieldTermStructure^ dividendTS, Cephei::QL::IQuote^ underlying)
{
    return REFNEW CLocalVolSurface ( blackTS,  riskFreeTS,  dividendTS,  underlying);
}
